package cn.e4j.modular.api.controller;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.IDto;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.util.LangUtils;
import cn.e4j.core.util.ToolUtil;
import cn.e4j.modular.api.service.GwbService;
import cn.e4j.modular.api.service.OuterUserService;
import cn.e4j.modular.api.util.ControllerSupport;
import cn.e4j.modular.crm.mapper.GwdhTMapper;
import cn.e4j.modular.system.entity.SysUser;
import cn.e4j.modular.system.mapper.SystemMapper;
import cn.e4j.modular.system.service.impl.SystemServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("api/wx/outerUser")
public class OuterUserController extends ControllerSupport {

    @Autowired
    OuterUserService outerUserService;

    @Autowired
    GwdhTMapper gwdhTMapper;

    @Autowired
    SystemServiceImpl systemService;

    @Autowired
    GwbService gwbService;

    //获取公告
    @PostMapping("getNotices")
    public ResponseData getNotices(@DtoParam DtoWapper wapper){

        Dto dto = wapper.getDto();
        SysUser user = wapper.getSysUser();
        dto.put("ID",user.getID());
        List<Dto> notices = null;
        try {
            Dto res = outerUserService.getAuthority(user, dto);

            res.put("GWDH006", LocalDate.now());//当前时间要大于过期时间 保证实时性

            notices = gwdhTMapper.getNotices(res);
        }catch (Exception e){
            e.printStackTrace();
            return ERROR;
        }

        return SUCCESS(notices);
    }

//    //获取商品（新品+爆款+促销）
//    @PostMapping("getProducts")
//    public ResponseData getProducts(@DtoParam DtoWapper wapper){
//        Dto result = new Dto();
//        Dto dto = wapper.getDto();
//        try {
//            //<!--查询商品 1试销 2上市 3.爆款 4.平稳 5退市-->
//            dto.put("GWAM028",2);
//            List<Dto> news = outerUserService.getProducts(dto);
//            dto.put("GWAM028",3);
//            List<Dto> hot = outerUserService.getProducts(dto);
//            //查询促销商品
//            dto.put("now", LocalDate.now());
//            dto.put("LANG", LangUtils.getLang(wapper.getSysUser().getLANG()));
//            List<Dto> promots = outerUserService.getPromoteProducts(dto);
//
//            result.put("news",news);
//            result.put("hot",hot);
//            result.put("promots",promots);
//        }catch (Exception e){
//            e.printStackTrace();
//            return ERROR;
//        }
//
//        return SUCCESS(result);
//    }

    //获取商品（新品+爆款+促销）
    @PostMapping("gethot")
    public ResponseData gethot(@DtoParam DtoWapper wapper){
        Dto result = new Dto();
        Dto dto = wapper.getDto();
        try {
            //<!--查询商品 1试销 2上市 3.爆款 4.平稳 5退市-->
            dto.put("GWAM028",3);
            List<Dto> hot = outerUserService.getProducts(dto);

            result.put("hot",hot);
        }catch (Exception e){
            e.printStackTrace();
            return ERROR;
        }

        return SUCCESS(result);
    }

    //获取商品（促销）
    @PostMapping("getPromotes")
    public ResponseData getPromotes(@DtoParam DtoWapper wapper){
        Dto result = new Dto();
        Dto dto = wapper.getDto();
        try {
            //查询促销商品
            dto.put("now", LocalDate.now());
            dto.put("LANG", LangUtils.getLang(wapper.getSysUser().getLANG()));
            List<Dto> promots = outerUserService.getPromoteProducts(dto);

            result.put("promots",promots);
        }catch (Exception e){
            e.printStackTrace();
            return ERROR;
        }

        return SUCCESS(result);
    }


    //获取订单 服务中 待评价 的数量
    @PostMapping("getCount")
    public ResponseData getCount(@DtoParam DtoWapper wapper){
        Dto inDto = wapper.getDto();
        SysUser user = wapper.getSysUser();
        //客户权限
        Dto gwb001 = gwbService.getGwb001(user, inDto);
        inDto.put("CUSTOMERS", gwb001.get("CUSTOMERS"));

        //暂时无组织权限
//        Dto sys003 = systemService.getSys003(user);
//        inDto.put("ORGS", sys003.get("ORGS"));

        inDto.put("GWCSACTI", "Y");

        Dto count = outerUserService.getCount(user, inDto);
        return SUCCESS(count);
    }

    //获取服务预约信息
    @PostMapping("getfwyy")
    public ResponseData getfwyy(@DtoParam DtoWapper wapper){
        Dto indto = wapper.getDto();
        SysUser user = wapper.getSysUser();
        indto.put("LANG", LangUtils.getLang(user.getLANG()));
        List<Dto> dtoList = outerUserService.getUnfinished(indto);
        return SUCCESS(dtoList);
    }

    //获取商品详情
    @PostMapping("getGoodsInfo")
    public ResponseData getGoodsInfo(@DtoParam DtoWapper wapper){
        List<Dto> dtoList = null;
        try {
            dtoList = outerUserService.getGoodsInfo(wapper.getPage(),wapper.getDto());
        }catch (Exception e){
            e.printStackTrace();
            return ERROR;
        }

        return SUCCESS(dtoList);
    }

}
